package mailgod;

import controlP5.ControlEvent;
import processing.core.PApplet;
import oscP5.*;
import netP5.*;

import circus.*;

public class MailGod extends PApplet {

	private static String MOTHER = "m";
	private static String CHILD = "c";
	public OscCloud cloud;
	public OscP5 network;
	 ProjectionMapper mapper;
	private SpiralRender spiralRender;
	//public String leerString = "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ";
	public String completeTweet = " ";
	//public String completeTweet = " Ldfjn jksdnfjn kjfdjfnadksjafndnfdsj fndkbdkfb dskdsfbdshaf dfjbdsh bdsfdsbh ajdsbfhjdsb f";
	/** 
	 * CONFIG
	 */
	private static String MODE = "settings";
	static int BEAMER = 0; 
	static String TYPE = CHILD;
	// xml
	
	
	
private GodTweets godTweet;
	public void setup() {
		size(1280,960,P3D);
		mapper = new ProjectionMapper(this, MODE, BEAMER, 1); //settings, sketch, projection
		spiralRender = new SpiralRender();
		spiralRender.init(this);
		if(TYPE==MOTHER){
			//cloud = new OscCloudEx(this, "parent", "10.254.255.205", 1337);
			cloud = new OscCloudEx(this, "parent", "10.254.255.212", 13370);
			//network = new OscP5(this,"10.254.255.212",7778);
			//network = new OscP5(this,"192.168.0.1",7778);
			godTweet = new GodTweets();
			godTweet.init(this);
			godTweet.start();
			//cloud.running = true;
		}else if(TYPE==CHILD){
		  //cloud = new OscCloudEx(this, "child", "10.254.255.205", 1338, "10.254.255.205", 1337);
			cloud = new OscCloudEx(this, "child", "192.168.0.2", 13380, "192.168.0.1", 13370);
		//	network = new OscP5(this,"10.254.255.212",7778);
		}
		smooth();
		//frameRate(20);
	}

	public void draw() 
	{
		update();
		if(mapper.ready && !mapper.config){
			//background(0);
			
			//mapper.textures[0] = get();
			spiralRender.draw();
		}
		background(2);

	}
	private void update()
	{
		if(TYPE==MOTHER){
			godTweet.update();
		}
	}
	
	public void broadcastTweet(String tweetAdd)
	{
		  OscMessage myOscMessage = new OscMessage("/msg");
		  myOscMessage.add(tweetAdd);
		  //myOscMessage.add("Lorem ipsum plum plum plum Lorem ipsum plum plum plum Lorem ipsum plum plum plum Lorem ipsum plum plum plum Lorem ipsum plum plum plum Lorem ipsum plum plum plum Lorem ipsum plum plum plumLorem ipsum plum plum plumLorem ipsum plum plum plum");
		  //network.send(myOscMessage);

		cloud.send("tMsg",tweetAdd);
		System.out.println("Mother says: "+tweetAdd);
	}
	public void incomingMessage(String message, String content){
		
		  if(TYPE==CHILD){
			  System.out.println("child gets: "+content);
			  completeTweet=content+completeTweet;
		  }
		  //System.out.println(MailGod.BEAMER+" : "+completeTweet);
	}
	
	public void oscEvent(OscMessage theOscMessage) {
		  cloud.oscEvent(theOscMessage);
		//if(theOscMessage.checkAddrPattern("/msg")==true) {
		if(TYPE==CHILD){
			  //System.out.println("child gets: "+theOscMessage.get(0).stringValue());
			  //completeTweet=theOscMessage.get(0).stringValue()+completeTweet;
		  }
		  
			    
		//	  }
		}
	public void controlEvent(ControlEvent theEvent) {
		  mapper.controlEvent(theEvent);
	}
	
	public static void main(String args[])
	{
		PApplet.main(new String[] { "--present", mailgod.MailGod.class.getName()});
	}
}
